package bar.dark.lee.setting.sms.smsinsert;

import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import bar.dark.lee.BDConstents;
import bar.dark.lee.common.BDUtil;
import bar.dark.lee.setting.sms.InputData;

public class SmsInMoney3 
{
	private int smsType; 
//	private boolean flagATM = false;
//	private boolean flagHalbu = false;
//	private String halbuMonth = "0";
//	
//	private boolean FLAG = true;
	private boolean available = false;
//	
//	private boolean outmoneyFlag = false;
//	
//	//1:check 2:credit
	private int checkcreditFlag = 0;
//	
	private final int CHECK = 1;
	private final int CREDIT = 2;
//	
	private String message;
	private InputData data;
//	
	private String date;
	
	public SmsInMoney3(){};
	
	public SmsInMoney3(InputData data, String message, String date, int smsType, int checkcreditFlag)
	{
		this.data = data;
		this.message = message;
		this.date = date;
		this.smsType = smsType;
		this.checkcreditFlag = checkcreditFlag;
		
		inMoney();
	}
	
	public boolean getAvailable()
	{
		return available;
	}
	
	public InputData getData()
	{
		return data;
	}
	
	public int getSmsType()
	{
		return smsType;
	}
	
	//TODO
	private void inMoney()
	{
		if ( (data.getName().equalsIgnoreCase("DGB")) )
		{
			/*
			 * DGB 03/07 21:59
				08013**0272
				출금 2,500
				잔액 1,633,954
				대백마트
				
				DGB 05/06 14:20
				17408*21931
				출금 36,000
				급식비
				
				DGB 05/31 02:26
				17408*21931
				출금 13,690
				잔액 263,068
				가스1305
			 */
			Pattern pName = Pattern.compile("(\\d{1,8}\\*{1,5}\\d{1,8})");
			Matcher mName = pName.matcher(message);
			Pattern pMoney = Pattern.compile("입금\\s*\\n*\\s*\\n*(\\d*,*\\d*,*\\d*,*\\d)\\s*\n*\\s*\\n*");
			Matcher mMoney = pMoney.matcher(message);
			Pattern pDate = Pattern.compile("(\\d{1,2}/\\d{1,2})");
			Matcher mDate = pDate.matcher(message);
			Pattern pMemo = Pattern.compile("잔액\\s*\\n*\\s*\\n*-*\\d*,*\\d*,*\\d*,*\\d\\s*\n*\\s*\\n*(.+$)");
			Matcher mMemo = pMemo.matcher(message);
			Pattern pMemo3 = Pattern.compile("입금\\s*\\n*\\s*\\n*\\d*,*\\d*,*\\d*,*\\d\\s*\n*\\s*\\n*(.+$)");
			Matcher mMemo3 = pMemo3.matcher(message);
			
			if (mName.find())
			{
				for (int i=0; i<mName.groupCount(); i++) 
				{
					data.setName("DGB" + "(" + mName.group(1) + ")");
				}
			}
			else
			{
				available = false;
				return;
			}
			
			if (mMoney.find()) 
			{
				for (int i=0; i<mMoney.groupCount(); i++) 
				{
					data.setMoney((mMoney.group(1).replaceAll(",", "")));
				}
			}
			else
			{
				available = false;
				return;
			}
			
			Calendar calendar = Calendar.getInstance();
			if (date != null && date.length() > 0)
			{
				data.setDate(date);
			} 
			else if (mDate.find()) 
			{
				for (int i=0; i<mDate.groupCount(); i++) 
				{
					data.setDate(calendar.get(Calendar.YEAR)+(mDate.group(1).replaceAll("/", "")));
				}
			}
			else
			{
				data.setDate(BDUtil.getDateDB(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH)+1, calendar.get(Calendar.DAY_OF_MONTH)));
			}
			
			if (mMemo.find()) 
			{
				for (int i=0; i<mMemo.groupCount(); i++) 
				{
					data.setMemo1((mMemo.group(1)).replaceAll("\\n", "").trim());
					data.setMemo2((mMemo.group(1)).replaceAll("\\n", "").trim());
					data.setMemo3((mMemo.group(1)).replaceAll("\\n", "").trim());
					data.setMemo4((mMemo.group(1)).replaceAll("\\n", "").trim());
				}
			}
			else if (mMemo3.find()) 
			{
				for (int i=0; i<mMemo3.groupCount(); i++) 
				{
					data.setMemo1((mMemo3.group(1)).replaceAll("\\n", "").trim());
					data.setMemo2((mMemo3.group(1)).replaceAll("\\n", "").trim());
					data.setMemo3((mMemo3.group(1)).replaceAll("\\n", "").trim());
					data.setMemo4((mMemo3.group(1)).replaceAll("\\n", "").trim());
				}
			}
			else
			{
				available = false;
				return;
			}
			
			available = true;
		}
		else if ( data.getName().indexOf("하이투자증권") > -1 )
		{
			/*
			 * -0242-05 계좌에서 8,400원 출금 (이마트울산점) 하이투자증권
				-2236-05 계좌로 1,169,000원 급여입금 (현대중공업(주)) 하이투자증권[FW]
				-2107-05 계좌에서 43,500원 출금 900010 하이투자증권
				-2107-05 계좌에서 43,500원 출금 (더리테일애셋강남) 하이투자증권
			 */
			Pattern pName = Pattern.compile("(-\\d{1,5}-\\d{1,5})");
			Matcher mName = pName.matcher(message);
			Pattern pMoney = Pattern.compile("계좌로\\s+(\\d*,*\\d*,*\\d*,*\\d)원\\s+");
			Matcher mMoney = pMoney.matcher(message);
			Pattern pDate = Pattern.compile("(\\d{1,2}/\\d{1,2})");
			Matcher mDate = pDate.matcher(message);
			Pattern pMemo = Pattern.compile("입금\\s+(.+)\\s+하이투자증권");
			Matcher mMemo = pMemo.matcher(message);
			
			if (mName.find())
			{
				for (int i=0; i<mName.groupCount(); i++) 
				{
					data.setName("하이투자증권" + "(" + mName.group(1) + ")");
				}
			}
			else
			{
				available = false;
				return;
			}
			
			if (mMoney.find()) 
			{
				for (int i=0; i<mMoney.groupCount(); i++) 
				{
					data.setMoney((mMoney.group(1).replaceAll(",", "")));
				}
			}
			else
			{
				available = false;
				return;
			}
			
			Calendar calendar = Calendar.getInstance();
			if (date != null && date.length() > 0)
			{
				data.setDate(date);
			} 
			else if (mDate.find()) 
			{
				for (int i=0; i<mDate.groupCount(); i++) 
				{
					data.setDate(calendar.get(Calendar.YEAR)+(mDate.group(1).replaceAll("/", "")));
				}
			}
			else
			{
				data.setDate(BDUtil.getDateDB(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH)+1, calendar.get(Calendar.DAY_OF_MONTH)));
			}
			
			if (mMemo.find()) 
			{
				for (int i=0; i<mMemo.groupCount(); i++) 
				{
					data.setMemo1(mMemo.group(1));
					data.setMemo2(mMemo.group(1));
					data.setMemo3(mMemo.group(1));
					data.setMemo4(mMemo.group(1));
				}
			}
			else
			{
				available = false;
				return;
			}
			
			available = true;
		}
		else if ( (data.getName().equalsIgnoreCase("한국투자")) )
		{
			/*
			 * [한국투자]
				김재은님
				8011****-21
				18,000원
				삼성카드체크출금
				(잔액: 177,600원)
				
				[한국투자] 10:36
				7874****-21
				이체출금
				200,000원
				농협 김은옥
				
				[한국투자] 01:35
				8017****-21
				이체출금
				100,000원
				아무개
			 */
			
			Pattern pName = Pattern.compile("(\\d{2,}\\*{2,}-\\d{2,})\\s+");
			Matcher mName = pName.matcher(message);
			Pattern pMoney = Pattern.compile("(\\d*,*\\d*,*\\d*,*\\d)원");
			Matcher mMoney = pMoney.matcher(message);
			Pattern pDate = Pattern.compile("(\\d{1,2}/\\d{1,2})");
			Matcher mDate = pDate.matcher(message);
			Pattern pMemo = Pattern.compile("\\d*,*\\d*,*\\d*,*\\d원\\s+(.+\\s*.+)\\s+\\(잔액");
			Matcher mMemo = pMemo.matcher(message);
			Pattern pMemo2 = Pattern.compile("\\d*,*\\d*,*\\d*,*\\d원\\s+(.+$)");
			Matcher mMemo2 = pMemo2.matcher(message);
			
			if (mName.find())
			{
				for (int i=0; i<mName.groupCount(); i++) 
				{
					data.setName("한국투자" + "(" + mName.group(1) + ")");
				}
			}
			else
			{
				available = false;
				return;
			}
			
			if (mMoney.find()) 
			{
				for (int i=0; i<mMoney.groupCount(); i++) 
				{
					data.setMoney((mMoney.group(1).replaceAll(",", "")));
				}
			}
			else
			{
				available = false;
				return;
			}
			
			Calendar calendar = Calendar.getInstance();
			if (date != null && date.length() > 0)
			{
				data.setDate(date);
			} 
			else if (mDate.find()) 
			{
				for (int i=0; i<mDate.groupCount(); i++) 
				{
					data.setDate(calendar.get(Calendar.YEAR)+(mDate.group(1).replaceAll("/", "")));
				}
			}
			else
			{
				data.setDate(BDUtil.getDateDB(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH)+1, calendar.get(Calendar.DAY_OF_MONTH)));
			}
			
			if (mMemo.find()) 
			{
				for (int i=0; i<mMemo.groupCount(); i++) 
				{
					data.setMemo1((mMemo.group(1)));
					data.setMemo2((mMemo.group(1)));
					data.setMemo3((mMemo.group(1)));
					data.setMemo4((mMemo.group(1)));
				}
			}
			else if (mMemo2.find()) 
			{
				for (int i=0; i<mMemo2.groupCount(); i++) 
				{
					data.setMemo1((mMemo2.group(1)));
					data.setMemo2((mMemo2.group(1)));
					data.setMemo3((mMemo2.group(1)));
					data.setMemo4((mMemo2.group(1)));
				}
			}
			else
			{
				available = false;
				return;
			}
			
			available = true;
		}
		else if ( (data.getName().equalsIgnoreCase("광주") ) )
		{
			/*
			 * 광주 05/23 18:56[184-107-XXX629]입         5,000잔     2,188,937  에이원
			 */
			Pattern pName = Pattern.compile("\\d{2}:\\d{2}\\[(\\d{1,}-\\d{1,}-.+)\\]입");
			Matcher mName = pName.matcher(message);
			Pattern pMoney = Pattern.compile("(\\d*,*\\d*,*\\d*,*\\d)잔");
			Matcher mMoney = pMoney.matcher(message);
			Pattern pDate = Pattern.compile("(\\d{2}/\\d{2})");
			Matcher mDate = pDate.matcher(message);
			Pattern pMemo = Pattern.compile("잔.*\\d*,*\\d*,*\\d*,*\\d\\s+(.+)");	// ???원 뒤에
			Matcher mMemo = pMemo.matcher(message);
			
			if (mName.find())
			{
				for (int i=0; i<mName.groupCount(); i++) 
				{
					data.setName(data.getName() + "(" + (mName.group(1)).replaceAll("-", "") + ")");
				}
			}
			else
			{
				available = false;
				return;
			}
			
			if (mMoney.find()) 
			{
				for (int i=0; i<mMoney.groupCount(); i++) 
				{
					data.setMoney((mMoney.group(1).replaceAll(",", "")));
				}
			}
			else
			{
				available = false;
				return;
			}
			
			Calendar calendar = Calendar.getInstance();
			if (date != null && date.length() > 0)
			{
				data.setDate(date);
			} 
			else if (mDate.find()) 
			{
				for (int i=0; i<mDate.groupCount(); i++) 
				{
					data.setDate(calendar.get(Calendar.YEAR)+(mDate.group(1).replaceAll("/", "")));
				}
			}
			else
			{
				data.setDate(BDUtil.getDateDB(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH)+1, calendar.get(Calendar.DAY_OF_MONTH)));
			}
			
			if (mMemo.find()) 
			{
				for (int i=0; i<mMemo.groupCount(); i++) 
				{
					data.setMemo1((mMemo.group(1)));
					data.setMemo2((mMemo.group(1)));
					data.setMemo3((mMemo.group(1)));
					data.setMemo4((mMemo.group(1)));
				}
			}
			else
			{
				available = false;
				return;
			}
			
			available = true;
		}
		else if ( (data.getName().equalsIgnoreCase("메리츠CMA")) )
		{
			/*
			 * [메리츠CMA]정광열님  ****-****-* *
				￦10,000 출금완료 잔액 ￦97,240
				
				[메리츠CMA]남상남님01/20 13:22 2253****16출금￦130,000 잔액 ￦2,208,972
				
				[메리츠CMA]홍길동님05/16 07:35
				2293****16출금￦500,000 잔액￦56,092
			 */
			
			Pattern pName = Pattern.compile("\\d{1,2}:\\d{1,2}\\s+(.{2,})\\s*입금");
			Matcher mName = pName.matcher(message);
			Pattern pMoney = Pattern.compile("입금\\s*.(\\d*,*\\d*,*\\d*,*\\d)");
			Matcher mMoney = pMoney.matcher(message);
			Pattern pDate = Pattern.compile("(\\d{1,2}/\\d{1,2})");
			Matcher mDate = pDate.matcher(message);
			Pattern pMemo = Pattern.compile("(입금\\s*.\\d*,*\\d*,*\\d*,*\\d)\\s+");
			Matcher mMemo = pMemo.matcher(message);
			
			if (mName.find())
			{
				for (int i=0; i<mName.groupCount(); i++) 
				{
					data.setName("메리츠CMA" + "(" + mName.group(1) + ")");
				}
			}
			else
			{
				available = false;
				return;
			}
			
			if (mMoney.find()) 
			{
				for (int i=0; i<mMoney.groupCount(); i++) 
				{
					data.setMoney((mMoney.group(1).replaceAll(",", "")));
				}
			}
			else if (mMoney.find()) 
			{
				for (int i=0; i<mMoney.groupCount(); i++) 
				{
					data.setMoney((mMoney.group(1).replaceAll(",", "")));
				}
			}
			else
			{
				available = false;
				return;
			}
			
			Calendar calendar = Calendar.getInstance();
			if (date != null && date.length() > 0)
			{
				data.setDate(date);
			} 
			else if (mDate.find()) 
			{
				for (int i=0; i<mDate.groupCount(); i++) 
				{
					data.setDate(calendar.get(Calendar.YEAR)+(mDate.group(1).replaceAll("/", "")));
				}
			}
			else
			{
				data.setDate(BDUtil.getDateDB(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH)+1, calendar.get(Calendar.DAY_OF_MONTH)));
			}
			
			if (mMemo.find()) 
			{
				for (int i=0; i<mMemo.groupCount(); i++) 
				{
					data.setMemo1((mMemo.group(1)));
					data.setMemo2((mMemo.group(1)));
					data.setMemo3((mMemo.group(1)));
					data.setMemo4((mMemo.group(1)));
				}
			}
			else
			{
				available = false;
				return;
			}
			
			available = true;
		}
		
		
		
		if (data.getName() == null || data.getName().equalsIgnoreCase(""))
		{
			available = false;
			return;
		}
		if (data.getDate() == null || data.getDate().equalsIgnoreCase(""))
		{
			available = false;
			return;
		}
		if (data.getMoney() == null || data.getMoney().equalsIgnoreCase(""))
		{
			available = false;
			return;
		}
		if ( (data.getMemo1() == null) || (data.getMemo1().equalsIgnoreCase("")) 
				&& ( (data.getMemo2() == null) || (data.getMemo2().equalsIgnoreCase("")) )
				&& ( (data.getMemo3() == null) || (data.getMemo3().equalsIgnoreCase("")) )
				&& ( (data.getMemo4() == null) || (data.getMemo4().equalsIgnoreCase("")) ) )
		{
			available = false;
			return;
		}
	}
		


}
